/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.control;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rf.sys.frm.BaseController;
import com.rf.sys.model.DepUser;
import com.rf.sys.model.Department;
import com.rf.sys.model.User;
import com.rf.sys.service.DepartmentService;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.JsonUtils;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.UuidUtil;
import com.rf.business.bean.CarBlacklistBean;
import com.rf.business.model.CarBlacklist;
import com.rf.business.model.Declaration;
import com.rf.business.service.CarBlacklistService;

@Controller
@RequestMapping("/bus/carBlacklist")
public class CarBlacklistController extends BaseController{
	/**
	 * 日志
	 */
	private static Logger log = LoggerFactory.getLogger(CarBlacklistController.class);
	private static final long serialVersionUID = -120161117110150119L;
	
	@Resource
	private CarBlacklistService carBlacklistService;

	@Resource
	private DepartmentService departmentService;
	/**
	 * 初始化页面信息
	 * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
	 */
	@RequestMapping(value = "index")
    public ModelAndView index (HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/business/carBlacklist/carBlacklist_list");
		return modelAndView; 
    }
    
    /**
	 * 搜索页面信息
	 * @param Page,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "query")
    @ResponseBody
    public void query (PageUtils pageUtils, CarBlacklist carBlacklist, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	//获取分页、查询参数，进行数据封装
    	Map searchMap = carBlacklistService.getCarBlacklistPage(carBlacklist, pageUtils);
    	super.responsePrint(request, response, JsonUtils.mapToJson(searchMap));
    }
    
    /**
     * 编辑页面初始化
     * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "edit")
    public ModelAndView edit (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	ModelAndView modelAndView = new ModelAndView();
		String id = request.getParameter("id");
		CarBlacklist carBlacklist = new CarBlacklist();
//		CarBlacklistBean carBlacklistBean = new CarBlacklistBean();
		if(id != null && !id.equals("")){
			carBlacklist = carBlacklistService.getCarBlacklistByID(id);
		}else{
//			carBlacklist.setState(ConfigUtils.ENABLESTATE);
		}
		modelAndView.addObject("carBlacklist", carBlacklist);
        modelAndView.setViewName("/business/carBlacklist/carBlacklist_edit");
        return modelAndView; 
    }
    
    /**
     * 保存数据信息
     * @param CarBlacklist,request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "save")
    public void save (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	Session session = SecurityUtils.getSubject().getSession();
    	String params = request.getParameter("data");
    	JSONObject jsonObject = JSONObject.fromObject(params);
    	CarBlacklist carBlacklist  = (CarBlacklist) JSONObject.toBean(jsonObject, CarBlacklist.class);
			if(carBlacklist !=null && carBlacklist.getId()!=null && !carBlacklist.getId().equals("")){
				//修改
				carBlacklistService.updateCarBlacklist(carBlacklist);
			}else{
				//新增
				carBlacklist.setId(UuidUtil.get32UUID());
				carBlacklist.setCreateTime(new Date());
				carBlacklist.setState(ConfigUtils.CARBLACKSTATE);
				carBlacklist.setCreateUser((String)session.getAttribute("userSessionId"));
				carBlacklistService.addCarBlacklist(carBlacklist);
			}
			returnObj.element("message", "success");//成功
			super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 删除数据信息
     * @param request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "delete")
    @ResponseBody
    public void delete (HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String ids = request.getParameter("ids");
    	String [] idsArr = ids.split(",");
    	for(int i=0;i<idsArr.length;i++){
    		carBlacklistService.deleteCarBlacklist(idsArr[i]);
    	}
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 更新数据状态
     * @param carBlacklist,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "updateState")
    @ResponseBody
    public void updateState (CarBlacklist carBlacklist, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	carBlacklistService.updateCarBlacklist(carBlacklist);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
}


